package com.tebiecloud.autoconfigure;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.tebiecloud.common.annotation.AnnotationScan;
import com.tebiecloud.common.context.SpringContextHolder;
import com.tebiecloud.common.handler.GlobalExceptionHandler;
import com.tebiecloud.common.handler.RestResponseErrorHandler;
import com.tebiecloud.common.filter.XssFilter;
import com.tebiecloud.common.gen.SnowflakeIdGenerator;
import com.tebiecloud.common.health.DbHealthIndicator;
import com.tebiecloud.common.mybatis.ModelMetaObjectHandler;
import com.tebiecloud.common.properties.CommonProperties;
import com.tebiecloud.common.properties.IdGenProperties;
import com.tebiecloud.common.security.SecurityRestTemplate;
import com.tebiecloud.common.security.oauth2.client.OAuth2ClientProperties;
import com.tebiecloud.common.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.client.RestTemplate;

/**
 * 默认配置类
 * @Author: tebie
 * @Date: 2019-08-04 11:08
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({CommonProperties.class,  IdGenProperties.class, OAuth2ClientProperties.class})
public class AutoConfiguration {

    /**
     * 拦截器
     * @return
     */
    @Bean
    public FilterRegistrationBean xxsFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean(new XssFilter());
        log.info("bean [{}]",bean);
        return bean;
    }

    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 默认加密配置
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(BCryptPasswordEncoder.class)
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        BCryptPasswordEncoder encoder =  new BCryptPasswordEncoder();
        log.info("bean [{}]",encoder);
        return encoder;
    }

    /**
     * Spring上下文工具配置
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(SpringContextHolder.class)
    public SpringContextHolder springContextHolder() {
        SpringContextHolder holder = new SpringContextHolder();
        log.info("bean [{}]",holder);
        return holder;
    }

    /**
     * 统一异常处理配置
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(GlobalExceptionHandler.class)
    public GlobalExceptionHandler exceptionHandler() {
        GlobalExceptionHandler exceptionHandler = new GlobalExceptionHandler();
        log.info("bean [{}]", exceptionHandler);
        return exceptionHandler;
    }

    /**
     * ID生成器配置
     * @param idGenProperties
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(IdGenProperties.class)
    public SnowflakeIdGenerator snowflakeIdWorker(IdGenProperties idGenProperties) {
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator(idGenProperties.getWorkId(), idGenProperties.getCenterId());
        log.info("bean [{}] properties [{}]", snowflakeIdGenerator, idGenProperties);
        return snowflakeIdGenerator;
    }

    /**
     * 自定义注解扫描
     * @param amqpTemplate
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(AnnotationScan.class)
    public AnnotationScan annotationScan(AmqpTemplate amqpTemplate) {
        AnnotationScan scan = new AnnotationScan(amqpTemplate);
        log.info("bean [{}]", scan);
        return scan;
    }

    /**
     * 自定义Oauth2请求类
     * @param commonProperties
     * @param busProperties
     * @param publisher
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(SecurityRestTemplate.class)
    public SecurityRestTemplate openRestTemplate(CommonProperties commonProperties, BusProperties busProperties, ApplicationEventPublisher publisher) {
        SecurityRestTemplate restTemplate = new SecurityRestTemplate(commonProperties,busProperties,publisher);
        //设置自定义ErrorHandler
        restTemplate.setErrorHandler(new RestResponseErrorHandler());
        log.info("bean [{}]", restTemplate);
        return restTemplate;
    }

    /**
     * 自定义RestTemplate类
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        //设置自定义ErrorHandler
        restTemplate.setErrorHandler(new RestResponseErrorHandler());
        return restTemplate;
    }

    /**
     * 自定义健康检查类
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(DbHealthIndicator.class)
    public DbHealthIndicator dbHealthIndicator() {
        DbHealthIndicator dbHealthIndicator = new DbHealthIndicator();
        return dbHealthIndicator;
    }

//    /**
//     * 自动填充模型数据
//     * @return
//     */
//    @Bean
//    @ConditionalOnMissingBean(ModelMetaObjectHandler.class)
//    public ModelMetaObjectHandler modelMetaObjectHandler() {
//        ModelMetaObjectHandler metaObjectHandler = new ModelMetaObjectHandler();
//        log.info("ModelMetaObjectHandler [{}]", metaObjectHandler);
//        return metaObjectHandler;
//    }

}
